<template>
  <div v-loading="loading" class="list-table">
    <el-form ref="queryForm" :model="queryParams" :inline="true" @submit.prevent.native>
      <el-form-item :label="$t('指标名称')" prop="keyName">
        <el-input
          v-model="queryParams.keyName"
          :placeholder="$t('指标名称')"
          clearable
          :onsubmit="false"
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('财报周期')" prop="type">
        <el-select v-model="queryParams.yearType" value="" :placeholder="$t('财报周期')" clearable>
          <el-option
            v-for="dict in pcwYearTypeOptions"
            :key="dict.id"
            :label="$t(dict.label)"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
        <el-button v-hasPermi="['deal:pcwMedical:view']" icon="el-icon-search" size="mini" @click="selectVSCompany">{{ $t('选择对比公司') }}</el-button>
      </el-form-item>
    </el-form>

    <el-table
      :data="pcwDataKeysList"
      border
      tooltip-effect="darkTable"
      height="400"
      class="pcwDataKeysTable"
    >
      <el-table-column v-for="(item, index) in baseTit" :key="index" :width="index < 2 ? 150 : ''" :min-width="index > 1 ? 150 : ''" :label="$t(item)">
        <template slot="header">
          <span :class="index > 1 ? 'alignRight' : ''">
            {{ $t(item) }}
          </span>
        </template>
        <template slot-scope="scope">
          <span v-if="index === 0">
            {{ $t(scope.row.keyReport) }}
          </span>
          <span v-if="index === 1">
            {{ $t(scope.row.keyName) }}
          </span>
          <span v-if="index > 1" class="alignRight">
            <div v-for="(map, ind) in scope.row.listMap" :key="ind">
              <div v-if="map.companyName === item">
                <span :class="map.riskType">{{ moneyFormat(map.keyValue) }}</span>
              </div>
            </div>
          </span>
        </template>
      </el-table-column>
    </el-table>

    <!-- 选择对比公司弹出窗 -->
    <el-dialog :title="$t('选择对比公司')" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealPcwCompanyContrastForm ref="dealPcwCompanyContrastForm" :company-id="companyId || this.$route.query.companyId" />
    </el-dialog>
  </div>
</template>

<script>
import dealPcwCompanyContrastForm from '@/views/deal/pcwmedical/dealPcwCompanyContrastForm'
import { findGroupCompanyVS, getPcwYearTypeData } from '@/api/deal/pcwmedical/dealPcwCompanyContrast'
export default {
  components: {
    dealPcwCompanyContrastForm
  },
  props: {
    companyId: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      // 表头展示
      baseTit: [],
      loading: true,
      // 财务数据基础数据集合
      pcwDataKeysList: [],
      // 查询参数
      queryParams: {
        companyId: this.companyId || this.$route.query.companyId,
        contrastId: '',
        keyName: '',
        yearType: ''
      },
      // 报告类型
      typeOptions: [],
      // 弹出层
      formOpen: false,
      // 财报年份-类型
      pcwYearTypeOptions: []
    }
  },
  watch: {
    companyId: {
      handler(val) {
        if (val) {
          this.queryParams.companyId = val
          this.getList()
          this.getYearTypeOptions()
        }
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    if (this.$route.query.companyId) {
      this.queryParams.companyId = this.$route.query.companyId
      this.getList()
      this.getYearTypeOptions()
    }
  },
  created() {
    // 财报周期
    this.getYearTypeOptions()
  },
  methods: {
    getList() {
      this.baseTit = []
      this.loading = true
      findGroupCompanyVS(this.queryParams).then(response => {
        this.pcwDataKeysList = response.rows
        this.baseTit = [this.$t('分类'), this.$t('指标名称')]
        if (this.pcwDataKeysList !== null && this.pcwDataKeysList.length > 0) {
          this.pcwDataKeysList[0].listMap.forEach(item => {
            this.baseTit.push(item.companyName)
          })
        }
        this.loading = false
      })
    },
    getYearTypeOptions() {
      getPcwYearTypeData(this.queryParams).then(response => {
        this.pcwYearTypeOptions = response.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        companyId: this.companyId || this.$route.query.companyId,
        year: undefined,
        type: undefined,
        attachmentId: undefined,
        createBy: undefined,
        createTime: undefined,
        updateBy: undefined,
        updateTime: undefined,
        delFlag: undefined,
        remark: undefined,
        yearS: undefined,
        yearE: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryHandle()
    },
    // 选择对比公司
    selectVSCompany() {
      this.formOpen = true
    },
    // 获取对比数据
    initVSData(companyIds) {
      if (companyIds !== '') {
        this.queryParams.contrastId = companyIds
        this.getList()
      }
    }
  }
}
</script>

